const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const root = process.cwd()
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

const config = {
  mode: 'production',
  // devtool: 'source-map',
  performance: {
    hints: "warning", // "error" 或者 false 都是有效值
    maxEntrypointSize: 600000,
    maxAssetSize: 300000,
  },
  entry: {
    index: './src/index',
    common: [
      path.resolve(root, './src/core/config'),
      path.resolve(root, './src/utils/listener'),
      // path.resolve(root, './src/core/video'),
      path.resolve(root, './src/core/shader'),
    ],
  },
  output: {
    filename: '[name].[hash:4].js',
    path: path.resolve(root, 'dist'),
    publicPath: '/'
  },
  resolve: {
    alias: {
      '@src': path.join(root, 'src'),
      '@static': path.join(root, 'static'),
    }
  },
  optimization: {
    splitChunks: {//分割代码块
      cacheGroups: {
        vendors: {
          //第三方依赖
          priority: 1,
          name: 'vendors',
          test: /node_modules/,
          chunks: 'initial',
          minSize: 100,
          minChunks: 1 //重复引入了几次
        },
        /*styles: {
            name: 'styles',
            test: /\.(less|css)$/,
            chunks: 'all',    // merge all the css chunk to one file
            enforce: true
        }*/
      }
    },
    // runtimeChunk: {
    //     name: 'manifest'
    // },
    minimize: true,
    minimizer: [
      new TerserPlugin({
        test: /\.js(\?.*)?$/i,
        terserOptions: {
          // 混淆，默认也是开的，mangle也是可以配置很多选项的，具体看后面的链接
          mangle: true,
          compress: {
            // 传true就是干掉所有的console.*这些函数的调用.
            drop_console: true,
            // 干掉那些debugger;
            drop_debugger: true,
            // 如果你要干掉特定的函数比如console.info ，又想删掉后保留其参数中的副作用，那用pure_funcs来处理
            // pure_funcs: ["console.log"]
          }
        }
      }),
    ],
  },

  module: {
    rules: [
      {
        test: /\.js[x]?$/,
        use: [{
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
          }
        }],
        exclude:/node_modules/,
        include: [
          path.resolve(root, 'src'),
          path.resolve(root, 'static'),
        ]
      },
      {
        test: /\.(less|css)$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'postcss-loader',
          'less-loader',
        ],
        exclude:/node_modules/,
        include: [
          path.resolve(root, 'src'),
          path.resolve(root, 'static'),
        ]
      },
      {
        test: /\.(png|jpg|gif|jpeg|webp|svg|eot|ttf|woff|woff2|gexf|htc)$/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 10240,
            esModule: false
          }
        }],
        exclude:/node_modules/,
        include: [
          path.resolve(root, 'src'),
          path.resolve(root, 'static'),
        ]
      },
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css"
    }),

    new webpack.ProvidePlugin({
      // Three: path.resolve(root, './static/libraries/three.min'),
      // video: path.resolve(root, './static/libraries/video'),
      // xor4096: path.resolve(root, './static/libraries/xor4096.min'),
      // Detector: path.resolve(root, './static/libraries/Detector'),
    }),

    new HtmlWebpackPlugin({
      title: 'danger',
      filename: 'index.html',
      template: './index.html',
      inject: 'body',
      chunks: ['index'],
      // 模块次序：手动
      chunksSortMode: 'manual',
    }),

    new webpack.DefinePlugin({
      'process.env': { NODE_ENV: '"production"' },
    }),

    new CopyWebpackPlugin({
      patterns: [{
        from: path.resolve(root, './static'),
        to: path.resolve(root, './dist/static'),
      }]
    }),
  ]
};

module.exports = config;
